#!/bin/sh
# shellcheck shell=dash disable=SC2169 source=/dev/null
#
# Prepares start of hs485d. 
#
# Writes /etc/config/InterfacesList.xml
# It must be started before webserver!
# It does NOT start hs485d itself! 
#

LOGLEVEL_HS485D=5

CFG_TEMPLATE_DIR=/etc/config_templates

[[ -r /var/hm_mode ]] && . /var/hm_mode

# skip this startup if not in normal mode
[[ "${HM_MODE}" != "NORMAL" ]] && exit 0

init() {
  # check that /etc/config/hs485d exists
  if [[ ! -d /etc/config/hs485d ]]; then
    mkdir -p /etc/config/hs485d
  fi

  # make sure to generate a copy of /etc/config/hs485d.conf in /var/etc
  # and modify things users should not change (e.g. port, etc).
  if [[ -e /etc/config/hs485d.conf ]]; then
    cp /etc/config/hs485d.conf /var/etc
    sed -i -e 's/^Listen\s\+Port\s*=.*$/Listen Port = 32000/' /var/etc/hs485d.conf
  fi

  # check for differences in /etc/config/InterfacesList.xml between
  # the template and the active file and adapt it accordingly.
  if [[ ! -e /etc/config/InterfacesList.xml ]]; then
    cp ${CFG_TEMPLATE_DIR}/InterfacesList.xml /etc/config
  else
    cmp -s ${CFG_TEMPLATE_DIR}/InterfacesList.xml /etc/config/InterfacesList.xml || cp ${CFG_TEMPLATE_DIR}/InterfacesList.xml /etc/config
  fi

  # if there is no HmIP support required we have to strip
  # the HmIP-RF part from InterfacesList.xml
  if [[ -z "${HM_HMIP_DEV}" ]]; then
    # remove HmIP-RF from InterfacesList.xml because not required (BidCos-RF only mode)
    line_start=$(grep -n '>HmIP-RF<' /etc/config/InterfacesList.xml | cut -d: -f1 | head -1)
    line_end=$(grep -n '>HmIP-RF<' /etc/config/InterfacesList.xml | cut -d: -f1 | tail -1)
    if [[ -n "${line_start}" && -n "${line_end}" ]]; then
      line_start=$((line_start-1))
      line_end=$((line_end+1))
      sed -i "${line_start},${line_end}d" /etc/config/InterfacesList.xml
    fi
  fi

  # if there is no BidCos-RF support required we have to strip
  # the BidCos-RF part from InterfacesList.xml
  if [[ -z "${HM_HMRF_DEV}" ]] && ( [[ ! -f /etc/config/rfd.conf ]] || ! grep -E -q "^Type = (HMLGW2|Lan Interface)" /etc/config/rfd.conf ); then
    # remove BidCos-RF from InterfacesList.xml because not required (HmIP-RFUSB only mode)
    line_start=$(grep -n '>BidCos-RF<' /etc/config/InterfacesList.xml | cut -d: -f1 | head -1)
    line_end=$(grep -n '>BidCos-RF<' /etc/config/InterfacesList.xml | cut -d: -f1 | tail -1)
    if [[ -n "${line_start}" && -n "${line_end}" ]]; then
      line_start=$((line_start-1))
      line_end=$((line_end+1))
      sed -i "${line_start},${line_end}d" /etc/config/InterfacesList.xml
    fi
  fi
}

start() {
  echo -n "Starting hs485dLoader: "
  init
  if grep -q "^\[Interface .\]" /var/etc/hs485d.conf 2>/dev/null; then
    # get the current syslog settings
    [[ -r /etc/config/syslog ]] && . /etc/config/syslog

    # adjust the oom score (which is inherited by start-stop-daemon)
    # to ensure that others are killed first in case of low memory situations
    echo -900 >/proc/$$/oom_score_adj 2>/dev/null

    /bin/hs485dLoader -l ${LOGLEVEL_HS485D} -ds -dd /var/etc/hs485d.conf
    echo "OK"
  else
    echo "disabled"
  fi
}

restart() {
  start
}

case "$1" in
  start)
    start
  ;;
  stop)
    # nothing
  ;;
  restart|reload)
    restart
  ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac

exit 0
